package com.rjwh.dingdong.client.mvp.presenter.impl;

import android.content.Context;

import com.aishua.lib.task.AsResult;
import com.orangegangsters.github.swipyrefreshlayout.library.SwipyRefreshLayoutDirection;
import com.rjwh.dingdong.client.R;
import com.rjwh.dingdong.client.bean.jsonbean.ResGetMyScoreRecordBean;
import com.rjwh.dingdong.client.bean.localbean.GetMyScoreRecordBean;
import com.rjwh.dingdong.client.bean.localbean.ScoreRecordListData;
import com.rjwh.dingdong.client.constant.NetConstant;
import com.rjwh.dingdong.client.mvp.BasePresenter;
import com.rjwh.dingdong.client.mvp.model.impl.MorePointsDetialModel;
import com.rjwh.dingdong.client.mvp.presenter.IMorePointsDetialPresenter;
import com.rjwh.dingdong.client.mvp.view.MorePointsDetialView;
import com.rjwh.dingdong.client.util.ToastUtil;

import java.util.List;

/**
 * @author Kay
 * @ClassName MorePointsDetialPresenter
 * @Description
 * @date 2016-9-28
 * @history 2016-9-28 author: description:
 */

public class MorePointsDetialPresenter extends BasePresenter<MorePointsDetialView> implements IMorePointsDetialPresenter {
    private MorePointsDetialModel model;
    private SwipyRefreshLayoutDirection mDirection;

    public MorePointsDetialPresenter(Context context, MorePointsDetialView mMvpView) {
        super(context, mMvpView);
        model = new MorePointsDetialModel(this);
    }

    /**
     * 获取积分明细
     */
    @Override
    public void doGetMyScoreRecord(int pageindex, int type, boolean isRefresh, SwipyRefreshLayoutDirection direction) {
        mDirection = direction;
        model.doGetMyScoreRecord(pageindex, type, isRefresh);
    }

    @Override
    public void onServerDataAcquired(int businessType, AsResult mAsResult, boolean isRefresh) {
        if (isViewAttached()) {
            switch (businessType) {
                case NetConstant.GETMYSCORERECORD:
                    getMvpView().setRefreshing(false);
                    ResGetMyScoreRecordBean resGetMyScoreRecordBean = (ResGetMyScoreRecordBean) mAsResult.getObj();
                    if (mAsResult.getResultCode() > NetConstant.MSG_CONNECT_SUCCESS && resGetMyScoreRecordBean != null && resGetMyScoreRecordBean.getData() != null) {
                        GetMyScoreRecordBean data = resGetMyScoreRecordBean.getData();
                        List<ScoreRecordListData> scorelist = data.getScorelist();
                        if (scorelist != null) {
                            if (scorelist.isEmpty()) {
                                getMvpView().setDirection(SwipyRefreshLayoutDirection.TOP);
                                if (mDirection == SwipyRefreshLayoutDirection.BOTTOM) {
                                    ToastUtil.showToast(getContext(), getContext().getString(R.string.empty_data_more));
                                } else {
                                    ToastUtil.showToast(getContext(), getContext().getString(R.string.empty_data));
                                }
                            } else {
                                getMvpView().setDirection(SwipyRefreshLayoutDirection.BOTH);
                            }
                            getMvpView().loadData(isRefresh, scorelist);
                        }
                    } else {
                        getMvpView().setDirection(SwipyRefreshLayoutDirection.TOP);
                        ToastUtil.showToast(getContext(), getContext().getString(R.string.refresh_error));
                    }
                    break;
                default:
                    break;
            }
        }
    }

    @Override
    public void detachView() {
        model.detachView();
        detachViews();
    }

}
